home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdButtonProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  5.0 KB  |  166 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Button Properties Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Neil Rashbrook.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var insertNew;
  38. var buttonElement;
  39.  
  40. // dialog initialization code
  41.  
  42. function Startup()
  43. {
  44.   if (!InitEditorShell())
  45.     return;
  46.  
  47.   gDialog = {
  48.     buttonType:       document.getElementById("ButtonType"),
  49.     buttonName:       document.getElementById("ButtonName"),
  50.     buttonValue:      document.getElementById("ButtonValue"),
  51.     buttonDisabled:   document.getElementById("ButtonDisabled"),
  52.     buttonTabIndex:   document.getElementById("ButtonTabIndex"),
  53.     buttonAccessKey:  document.getElementById("ButtonAccessKey"),
  54.     MoreSection:      document.getElementById("MoreSection"),
  55.     MoreFewerButton:  document.getElementById("MoreFewerButton"),
  56.     RemoveButton:     document.getElementById("RemoveButton")
  57.   };
  58.  
  59.   // Get a single selected button element
  60.   var tagName = "button";
  61.   buttonElement = editorShell.GetSelectedElement(tagName);
  62.  
  63.   if (buttonElement)
  64.     // We found an element and don't need to insert one
  65.     insertNew = false;
  66.   else
  67.   {
  68.     insertNew = true;
  69.  
  70.     // We don't have an element selected,
  71.     //  so create one with default attributes
  72.  
  73.     buttonElement = editorShell.CreateElementWithDefaults(tagName);
  74.     if (!buttonElement)
  75.     {
  76.       dump("Failed to get selected element or create a new one!\n");
  77.       window.close();
  78.       return;
  79.     }
  80.     // Hide button removing existing button
  81.     gDialog.RemoveButton.setAttribute("hidden", "true");
  82.   }
  83.  
  84.   // Make a copy to use for AdvancedEdit
  85.   globalElement = buttonElement.cloneNode(false);
  86.  
  87.   InitDialog();
  88.  
  89.   InitMoreFewer();
  90.  
  91.   gDialog.buttonType.focus();
  92.  
  93.   SetWindowLocation();
  94. }
  95.  
  96. function InitDialog()
  97. {
  98.   var type = globalElement.getAttribute("type");
  99.   var index = 0;
  100.   switch (type)
  101.   {
  102.     case "button":
  103.       index = 2;
  104.       break;
  105.     case "reset":
  106.       index = 1;
  107.       break;
  108.   }
  109.   gDialog.buttonType.selectedIndex = index;
  110.   gDialog.buttonName.value = globalElement.getAttribute("name");
  111.   gDialog.buttonValue.value = globalElement.getAttribute("value");
  112.   gDialog.buttonDisabled.setAttribute("checked", globalElement.hasAttribute("disabled"));
  113.   gDialog.buttonTabIndex.value = globalElement.getAttribute("tabindex");
  114.   gDialog.buttonAccessKey.value = globalElement.getAttribute("accesskey");
  115. }
  116.  
  117. function RemoveButton()
  118. {
  119.   RemoveElementKeepingChildren(buttonElement);
  120.   SaveWindowLocation();
  121.   window.close();
  122. }
  123.  
  124. function ValidateData()
  125. {
  126.   var attributes = {
  127.     type: ["", "reset", "button"][gDialog.buttonType.selectedIndex],
  128.     name: gDialog.buttonName.value,
  129.     value: gDialog.buttonValue.value,
  130.     tabindex: gDialog.buttonTabIndex.value,
  131.     accesskey: gDialog.buttonAccessKey.value
  132.   };
  133.   for (var a in attributes)
  134.   {
  135.     if (attributes[a])
  136.       globalElement.setAttribute(a, attributes[a]);
  137.     else
  138.       globalElement.removeAttribute(a);
  139.   }
  140.   if (gDialog.buttonDisabled.checked)
  141.     globalElement.setAttribute("disabled", "");
  142.   else
  143.     globalElement.removeAttribute("disabled");
  144.   return true;
  145. }
  146.  
  147. function onAccept()
  148. {
  149.   // All values are valid - copy to actual element in doc or
  150.   //   element created to insert
  151.   ValidateData();
  152.  
  153.   editorShell.CloneAttributes(buttonElement, globalElement);
  154.  
  155.   if (insertNew && !InsertElementAroundSelection(buttonElement))
  156.   {
  157.     buttonElement.innerHTML = editorShell.GetContentsAs("text/html", 1); // OutputSelectionOnly (see nsIDocumentEncoder.h)
  158.     editorShell.InsertElementAtSelection(buttonElement, true);
  159.   }
  160.  
  161.   SaveWindowLocation();
  162.  
  163.   return true;
  164. }
  165.  
  166.